package com.stylefeng.guns.rest.modular.content.service;

import com.baomidou.mybatisplus.mapper.Condition;
import com.stylefeng.guns.rest.common.persistence.dao.ArtitleMapper;
import com.stylefeng.guns.rest.common.persistence.dao.ContentMapper;
import com.stylefeng.guns.rest.common.persistence.model.Article;
import com.stylefeng.guns.rest.common.persistence.model.Content;
import com.stylefeng.guns.rest.common.persistence.model.Message;
import com.stylefeng.guns.rest.common.persistence.model.User;
import com.stylefeng.guns.rest.common.utils.jpush.JpushUtils;
import com.stylefeng.guns.rest.modular.artitle.controller.dto.ArticleDetailResponse;
import com.stylefeng.guns.rest.modular.attent.controller.dto.AttentHotRequest;
import com.stylefeng.guns.rest.modular.catalogy.controller.dto.ArticleListResponse;
import com.stylefeng.guns.rest.modular.content.controller.dto.CommentListRequest;
import com.stylefeng.guns.rest.modular.content.controller.dto.CommentListResponse;
import com.stylefeng.guns.rest.modular.content.controller.dto.MyCommentList;
import com.stylefeng.guns.rest.modular.content.controller.dto.ParentComment;
import com.stylefeng.guns.rest.modular.keyword.controller.dto.KeywordRequestBody;
import com.stylefeng.guns.rest.modular.message.service.MessageService;
import com.stylefeng.guns.rest.modular.users.controller.dto.UserInfoRequest;
import com.stylefeng.guns.rest.modular.users.service.UserService;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by ASUS-PC on 2018/6/26.
 */
@Service
public class ContentService {
    @Autowired
    ContentMapper contentMapper;
    @Autowired
    MessageService messageService;
    @Autowired
    UserService userService;

    //获取评论数量
    public int queryVideoContentNums(int artitleId){
        return contentMapper.selectList(Condition.create().eq("title_id",artitleId)).size();
    }

    //插入评论消息
    public int insertContent(Content content){
        User sendUser = userService.selectUserByUserId(content.getTitleUser());
        User zanUser = userService.selectUserByUserId(content.getUserId());//点赞用户id
        //插入消息
        Message message = new Message();
        message.setType(2);
        message.setyUser(content.getTitleUser());
        message.setcUser(content.getUserId());
        message.setCreateTime(new Date());
        message.setContent(content.getContent());
        messageService.inserData(message);

        //发送极光推送
        JpushUtils.sendPushMessageByAlias(sendUser.getPhone(),zanUser.getNickName(),2);

        return contentMapper.insert(content);
    }

    //获取评论列表
    public List<CommentListResponse> getTitleCommentList(CommentListRequest request){
        Map<String ,Object> data = new HashedMap();
        data.put("titleId",request.getTitleId());
        data.put("pageIndex",(request.getPage()-1)*request.getSize());
        data.put("pageSize",request.getSize());
        return contentMapper.getCommentByTitleId(data);
    }
    //获取文章评论总数
    public int getCommentAllNums(int titleId){
        Map<String ,Object> data = new HashedMap();
        data.put("titleId",titleId);
        return contentMapper.getCommentAllNums(data).size();
    }

    //获取我的评论列表
    public List<MyCommentList> getMyCommentList(AttentHotRequest request){
        Map<String ,Object> data = new HashedMap();
        data.put("userId",request.getUserId());
        data.put("pageIndex",(request.getPage()-1)*request.getSize());
        data.put("pageSize",request.getSize());
        return contentMapper.getMyContentList(data);
    }

    //获取某条评论内容
    public ParentComment getTitleCommentById(int  id){
        Map<String ,Object> data = new HashedMap();
        data.put("id",id);
        return contentMapper.getTitleCommentById(data);
    }

    //获取评论的回复
    public List<MyCommentList> getReplyMyCommentList(AttentHotRequest request){
        Map<String ,Object> data = new HashedMap();
        data.put("userId",request.getUserId());
        data.put("pageIndex",(request.getPage()-1)*request.getSize());
        data.put("pageSize",request.getSize());
        return contentMapper.getMyReplyContentList(data);
    }

    //删除回复
    public boolean deletCommentById(int id){
        return contentMapper.delete(Condition.create().eq("id" , id))>0 ? true:false;
    }
    //删除视频的所有回复
    public void deleteAllContentByArtitleId(int titleId){
        contentMapper.delete(Condition.create().eq("title_id" , titleId));
    }

}
